% 1. Generate the sequence
% 2. Add blanks and targets
% 3. Generate transition sequence
% 4. Evaluate Detection Powers and Counterbalance quality


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% DEFINE PARAMETERS
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%profile on
clear all;
nIter = 5;

SOA = 2100;
seqLen = 120;
%period_labels = [15 20 25];
period_labels = 30;

numTargets = 2;
refreshBlockAfterBlanks = 4;
refreshBlockAfterTargets = 4;

minimum_acceptable_detection_power_for_direct_effects = 0.4;
minimum_acceptable_detection_power_for_1back_adaptation = 0.65;
minimum_acceptable_quality_of_counterbalance_for_n2 = 1.0;
minimum_acceptable_quality_of_counterbalance_for_n3 = 0.85;
minimum_acceptable_quality_of_counterbalance_for_n4 = 0.0;
minimum_acceptable_quality_of_counterbalance_for_n5 = 0.0;


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% INITIALIZE VARIABLES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

number_of_attempts_so_far = 0;

directEffect_DP = zeros(1,nIter);
adaptation_DP = zeros(1,nIter);
counterbalance_n2 = zeros(1,nIter);
counterbalance_n3 = zeros(1,nIter);
counterbalance_n4 = zeros(1,nIter);
counterbalance_n5 = zeros(1,nIter);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% SEARCH FOR SEQUENCES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i = 1:nIter
    
    all_conditions_met = false;
    
    while all_conditions_met == false
        [baseSeq, repeat_yesno, guideFunction] = ProbabilityRepetition_sequence(seqLen,period_labels);
        [newSeqString fullSeq] = addBlanksAndTargets(baseSeq+1, SOA, numTargets, refreshBlockAfterBlanks, refreshBlockAfterTargets);
        fullSeq = fullSeq(:);
        
        % Calculate some quality parameters of the sequence
        direct_effects_sequence = fullSeq;
        direct_effects_sequence(direct_effects_sequence<=0) = -1;
        adaptation_sequence = transitionSeq(fullSeq);
        
        detectPower_direct_effects = evalSeq(direct_effects_sequence,SOA);
        detectPower_1back_adaptation = evalSeq(adaptation_sequence,SOA);
        counterbalance_quality_n2 = counterbalance_quality(baseSeq,2);
        counterbalance_quality_n3 = counterbalance_quality(baseSeq,3);
        counterbalance_quality_n4 = counterbalance_quality(baseSeq,4);
        counterbalance_quality_n5 = counterbalance_quality(baseSeq,5);
        
        all_conditions_met = (counterbalance_quality_n2 >= minimum_acceptable_quality_of_counterbalance_for_n2) ...
            && (counterbalance_quality_n3 >= minimum_acceptable_quality_of_counterbalance_for_n3) ...
            && (counterbalance_quality_n4 >= minimum_acceptable_quality_of_counterbalance_for_n4) ...
            && (counterbalance_quality_n5 >= minimum_acceptable_quality_of_counterbalance_for_n5) ...
            && (detectPower_direct_effects >= minimum_acceptable_detection_power_for_direct_effects) ...
            && (detectPower_1back_adaptation >= minimum_acceptable_detection_power_for_1back_adaptation);
        
        number_of_attempts_so_far = number_of_attempts_so_far + 1;
        display(sprintf('Attempt #%d',number_of_attempts_so_far));
    end
    
    directEffect_DP(i) = detectPower_direct_effects;
    adaptation_DP(i) = detectPower_1back_adaptation;
    counterbalance_n2(i) = counterbalance_quality_n2;
    counterbalance_n3(i) = counterbalance_quality_n3;
    counterbalance_n4(i) = counterbalance_quality_n4;
    counterbalance_n5(i) = counterbalance_quality_n5;
    
    %plot(guideFunction); hold on; plot(baseSeq,'r'); hold off;
    %display([detectPower_direct_effects detectPower_1back_adaptation counterbalance_quality_n2 counterbalance_quality_n3 counterbalance_quality_n4 counterbalance_quality_n5]);
    %pause;
    
    
    display(sprintf('\n          Detection Powers'))
    display(sprintf('Direct Effect     D1back_adaptation'))
    display(sprintf('   %f           %f\n', detectPower_direct_effects, detectPower_1back_adaptation))
    display(sprintf('    Counterbalance quality index'))
    display(sprintf('   n=2      n=3      n=4      n=5   '))
    display(sprintf('%f %f %f %f\n',counterbalance_quality_n2,counterbalance_quality_n3,counterbalance_quality_n4,counterbalance_quality_n4))
    
    save(['./sequences/shortTerm' num2str(i) '.mat']);
    
end

%profile viewer